﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ManyConsole;
using TheTVDB;

namespace Processor
{
   namespace TVDBCommands
   {
      public class FindSeries : ConsoleCommand
      {
         public string seriesName;
         public bool QuietMode;

         public FindSeries()
         {
            this.IsCommand("FindSeries", "Finds a series from TVDB.");
            this.SkipsCommandSummaryBeforeRunning();

            HasAdditionalArguments(1, "<SeriesName>");
         }

         public override int Run(string[] remainingArguments)
         {
            seriesName = remainingArguments[0];

            TVDBApi db = new TVDBApi();

            if (QuietMode)
               db.SuppressOutput = true;

            foreach (TVDBApi.SeriesSearchResultItem item in db.FindSeries(seriesName))
               Console.WriteLine(string.Format("ID: [{0}] - Language [{1}] - Name: [{2}]", item.SeriesID, item.Language, item.Name));

            return 0;
         }
      }

      public class GetSeasonAndEpisodeByProductionCode : ConsoleCommand
      {
         public int seriesID;
         public int productionCode;
         public bool QuietMode;

         public GetSeasonAndEpisodeByProductionCode()
         {
            this.IsCommand("GetSeasonAndEpisodeByProductionCode", "Gets the season number, episode number, and episode name from a production code.");
            this.SkipsCommandSummaryBeforeRunning();

            HasAdditionalArguments(2, "<SeriesID> <ProductionCode>");
         }

         public override int Run(string[] remainingArguments)
         {
            seriesID = int.Parse(remainingArguments[0]);
            productionCode = int.Parse(remainingArguments[1]);

            int seasonNumber = 0;
            int episodeNumber = 0;
            string episodeName = string.Empty;

            TVDBApi db = new TVDBApi();

            if (QuietMode)
               db.SuppressOutput = true;

            db.GetSeasonAndEpisode(db.GetSeriesByID(seriesID), productionCode, ref seasonNumber, ref episodeNumber, ref episodeName);

            Console.WriteLine(string.Format("Season: [{0}], Episode: [{1}], Name: [{2}] ", seasonNumber, episodeNumber, episodeName));

            return 0;
         }
      }

      public class GetSeasonAndEpisodeByEpisodeName : ConsoleCommand
      {
         public int seriesID;
         public string currentEpisodeName;
         public bool QuietMode;

         public GetSeasonAndEpisodeByEpisodeName()
         {
            this.IsCommand("GetSeasonAndEpisodeByEpisodeName", "Gets the season number, episode number, and episode name from an episode name.");
            this.SkipsCommandSummaryBeforeRunning();

            HasAdditionalArguments(2, "<SeriesID> <EpisodeName>");
         }

         public override int Run(string[] remainingArguments)
         {
            seriesID = int.Parse(remainingArguments[0]);
            currentEpisodeName = remainingArguments[1];

            int seasonNumber = 0;
            int episodeNumber = 0;
            string episodeName = string.Empty;

            TVDBApi db = new TVDBApi();

            if (QuietMode)
               db.SuppressOutput = true;

            db.GetSeasonAndEpisode(db.GetSeriesByID(seriesID), currentEpisodeName, ref seasonNumber, ref episodeNumber, ref episodeName);

            Console.WriteLine(string.Format("Season: [{0}], Episode: [{1}], Name: [{2}] ", seasonNumber, episodeNumber, episodeName));

            return 0;
         }
      }
   }
}
